package com.stlm2.controller;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.entity.IntegralOrder;
import com.stlm2.dems.entity.OrderState;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.IntegralOrderService;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Author：fuchunyu
 * Date：2017/12/20
 * Description：
 */
@Controller
@RequestMapping("integralOrder")
public class IntegralOrderController {

    @Resource
    private UserService userService;
    @Resource
    private IntegralOrderService integralOrderService;

    //积分商品列表页
    @RequestMapping("/list")
    public String list() {
        return "/integralOrder/list";
    }



    @RequestMapping(value="/details",produces="application/json;charset=UTF-8")
    public ModelAndView edit(ModelAndView modelAndView, String id){
        modelAndView.addObject("id",id);
        modelAndView.setViewName("/integralOrder/detail");
        return modelAndView;
    }

    /**
     * @param page  页数
     * @param token 用户标识
     * @param code  订单编号
     * @param state 订单状态
     * @return 积分订单查询
     */
    @ResponseBody
    @RequestMapping(value = "/search.json", produces = "application/json;charset=utf-8")
    public String search(ModelMap model, String page, String token, String code, Integer state) {

        int pageNo = 1;
        int pageSize = 20;
        if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
            pageNo = NumberUtils.toInt(page);
        }
        if (StringUtils.isEmpty(token)) {
            return CodeContent.returnLogin();
        }
        User loginUser = userService.getByToken(token);
        if (loginUser == null) {
            return CodeContent.returnLogin();
        }
        IntegralOrder integralOrder = new IntegralOrder();

        if (!StringUtils.isEmpty(code)) {
            integralOrder.setCode(code.trim());
        }

        if (!StringUtils.isEmpty(state)) {
            integralOrder.setOrderStateValue(state);
        }
        PageView pageview = integralOrderService.getPage(pageNo, pageSize, integralOrder);
        if (pageview != null) {
            model.put("datas", integralOrderService.convertModel(pageview.getRecords()));
            model.put("recordsTotal", pageview.getRowCount());
            model.put("totalPage", pageview.getPageCount());
            model.put("pageNow", pageview.getPageNow());
            model.put("code",1);
            model.put("msg","success");
        }
        return  new JSONObject(model).toString();
    }

    /**
     * @param id               订单ID
     * @param token            用户标识
     * @param expressTypeValue 物流ID
     * @param wayBillNum       物流单号
     * @return 已发货订单
     */
    @ResponseBody
    @RequestMapping(value = "/shipments.json", produces = "application/json;charset=utf-8")
    public String update(Integer id, String token, Integer expressTypeValue, String wayBillNum) {
        User user = userService.getByToken(token);
        if (user == null)
            return CodeContent.returnLogin();
        IntegralOrder integralOrder = integralOrderService.findEntityById(id);
        if (integralOrder == null)
            return CodeContent.returnFailure("没有找到订单！");
        if (StringUtils.isEmpty(expressTypeValue))
            return CodeContent.returnFailure("物流为空！");
        if (StringUtils.isEmpty(wayBillNum))
            return CodeContent.returnFailure("物流单号为空！");
        integralOrder.setExpressTypeValue(expressTypeValue);
        integralOrder.setWayBillNum(wayBillNum);
        integralOrder.setOrderState(OrderState.Deliveried);
        integralOrderService.updateEntity(integralOrder);
        return CodeContent.returnSuccess(null);
    }

    /**
     *
     * @param id   订单ID
     * @param token  用户标识
     * @return  订单详情
     */
    @ResponseBody
    @RequestMapping(value = "/detail.json", produces = "application/json;charset=utf-8")
    public String detail(Integer id,String token){
        User user=userService.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();
        IntegralOrder integralOrder=integralOrderService.findEntityById(id);
        if(integralOrder==null)
            return CodeContent.returnFailure("订单查询失败！");
        ModelMap modelMap=integralOrderService.buildModel(integralOrder);
        return CodeContent.returnSuccess(modelMap);
    }

}
